home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 25 / Cream of the Crop 25.iso / utility / ffe101.zip / SOUND.SWG / 0033_PS16 (Protracker Studio 16).pas < prev    next >
Pascal/Delphi Source File  |  1997-03-02  |  20KB  |  358 lines

  1. Below lists the format of the Protracker Studio 16 (PS16) header.  This can
  2. change if it doesn't allow for enough flexibility for the time being.
  3.  
  4. ╒══════════════════════════════════════════════════════════════════════════════╕
  5. │ PS16 Header Format:                                                          │
  6. ╘══════════════════════════════════════════════════════════════════════════════╛
  7. Description         Length  Data Type  Information
  8. ──────────────────  ──────  ─────────  ─────────────────────────────────────────
  9. Sig                      5  Character  This contains the string "PS16■" which
  10.                                          acts as both an identifier to
  11.                                          determine the file, but also serves
  12.                                          as part of the text string which
  13.                                          can be done by TYPEing the file to
  14.                                          the screen.
  15. SongName                75  Character  This contains the 75-character name of
  16.                                          the music file.  This may contain
  17.                                          the MODule name if converted from a
  18.                                          MOD, or it may contain information
  19.                                          on number of channels or whatever.
  20.                                          The final byte, 75, MUST have a ^Z
  21.                                          in it.
  22. TypeOfFile               1       Byte  This field holds what type of music
  23.                                          file the file is.  If this field
  24.                                          contains a 0, then it is a module
  25.                                          with self-contained instruments.
  26.                                          If it is a 1, then it is a song
  27.                                          with the header and patterns, but
  28.                                          not the samples.
  29. CommentOfs               4    Longint  The Comment Offset field points to
  30.                                          the offset of the file where
  31.                                          comments begin.  The comments
  32.                                          contain all text and information
  33.                                          pertinent to the music.  The
  34.                                          sample names are also stored here.
  35.                                          See below for parsing information.
  36.                                          This field DOESN'T need to be
  37.                                          used.  Just store a zero here if
  38.                                          there are no comments.
  39. Version                  1       Byte  This is the version identifier for
  40.                                          the PS16 file.  All versions of
  41.                                          PS16 formats will have the header
  42.                                          structure as described thus far.
  43.                                          The format from here on can change,
  44.                                          although it isn't likely.  The
  45.                                          format described below is a Version
  46.                                          0 format.
  47.  
  48. ──────────────────────────────────────────────────────────── Version 0 Header
  49. NumPatterns              1       Byte  Contains the number of patterns
  50.                                          (max 256) in the file.
  51. totalPatternSize         4    Longint  For quick reads off the disk, the
  52.                                          TOTAL pattern length is stored here.
  53.                                          The maximum total size of patterns
  54.                                          for any given music file is
  55.                                          3075 bytes per pattern * 256 patterns.
  56.                                          With the current player, this is
  57.                                          simply not possible.
  58. SongLen                  1       Byte  This contains the number of sequences
  59.                                          in the PS16 file.
  60. Sequences              128       Byte  These are the sequences for the entire
  61.                                          PS16 music.
  62. Samples                 31*PS16Sample  These are the 31 individual sample
  63.                                          structures, described below.
  64.  
  65.  
  66. ╒══════════════════════════════════════════════════════════════════════════════╕
  67. │ PS16 Sample Header Format, Version 0:                                        │
  68. ╘══════════════════════════════════════════════════════════════════════════════╛
  69. Description         Length  Data Type  Information
  70. ──────────────────  ──────  ─────────  ─────────────────────────────────────────
  71. BitStruc                 1  Bit Field  These are bits that may be set for each
  72.                                          sample.  The default for a MOD file
  73.                                          is:  Bit 0 - Digital, Bit 1 - 0,
  74.                                               Bit 2 - 8-bit.
  75.            7 6 5 4 3 2 1 0
  76.                      │ │ └─ 0 - Digital, 1 - Synthesized
  77.                      │ └─── (if bit 0=1) 0 - FM, 1 - Waveform
  78.                      └───── 0 - 8-bit, 1 - 16-bit.
  79.  
  80. Volume                   1       Byte  Default volume for the sample.  Range
  81.                                          is 0 to 64 decimal.  Please, ALWAYS
  82.                                          make it default to this range.
  83. FineTune                 1       Byte  This field must contain one of the
  84.                                          following values:
  85.                                             0 - Tuning 0
  86.                                             1 - Tuning 1
  87.                                             2 - Tuning 2
  88.                                             3 - Tuning 3
  89.                                             4 - Tuning 4
  90.                                             5 - Tuning 5
  91.                                             6 - Tuning 6
  92.                                             7 - Tuning 7
  93.                                             8 - Tuning -8
  94.                                             9 - Tuning -7
  95.                                            10 - Tuning -6
  96.                                            11 - Tuning -5
  97.                                            12 - Tuning -4
  98.                                            13 - Tuning -3
  99.                                            14 - Tuning -2
  100.                                            15 - Tuning -1
  101. Length                   4    Longint  This contains the length of the
  102.                                          sample.  Currently, my player
  103.                                          doesn't support greater than 64k
  104.                                          samples, and I really don't see
  105.                                          the need.  It's here in case it's
  106.                                          needed.
  107. Repeat                   4    Longint  This contains the repeat start for
  108.                                          the sample.  Again, this can be
  109.                                          >64k
  110. RepeatLength             4    Longint  This contains the repeat length for
  111.                                          the sample.  If the sample Length
  112.                                          is greater than the sample Repeat
  113.                                          plus the sample RepLen, then the
  114.                                          sample can be clipped to Repeat+
  115.                                          RepLen.  If you didn't get that,
  116.                                          don't worry about it.
  117. C-2 Freq                 2       Word  The default playback frequency of
  118.                                          a sample's C on octave 2.  This
  119.                                          can be used to fine tune a sample,
  120.                                          but it defaults to 8448.
  121.  
  122.  
  123. ╒══════════════════════════════════════════════════════════════════════════════╕
  124. │ PS16 Pattern Format, Version 0:                                              │
  125. ╘══════════════════════════════════════════════════════════════════════════════╛
  126. Immediately following the version 0 header structure are the patterns.  The
  127. reason that the offset of the patterns in the file is not contained in the
  128. header is because it was felt that the patterns would probably not be
  129. relocated.
  130.  
  131. When converting from the MODule format, this format will always be a minimum
  132. of 253 bytes smaller than the patterns in a MODule.  A MODule's pattern is
  133. always 1,024 bytes.  This is obvious since a MODule note and effect occupies
  134. four bytes, and there are four channels per row.  With 64 rows in a file,
  135. this equation becomes:  4 bytes * 4 channels * 64 rows = 1,024 bytes.
  136.  
  137. A PS16 note occupies three bytes.  A "note" in PS16 consists of the actual
  138. note, an instrument, and a special effect and its data.
  139.  
  140. A PS16 note will use the following table to convert from Amiga periods to
  141. its own notes, and they will be stored as such:
  142.  ┌─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┐
  143.  │    1│    2│    3│    4│    5│    6│    7│    8│    9│   10│   11│   12│
  144.  │ 1712│ 1616│ 1524│ 1440│ 1356│ 1280│ 1208│ 1140│ 1076│ 1016│  960│  906│
  145.  │  C-0│  C#0│  D-0│  D#0│  E-0│  F-0│  F#0│  G-0│  G#0│  A-0│  A#0│  B-0│
  146.  ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤
  147.  │   13│   14│   15│   16│   17│   18│   19│   20│   21│   22│   23│   24│
  148.  │  856│  808│  762│  720│  678│  640│  604│  570│  538│  508│  480│  453│
  149.  │  C-1│  C#1│  D-1│  D#1│  E-1│  F-1│  F#1│  G-1│  G#1│  A-1│  A#1│  B-1│
  150.  ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤
  151.  │   25│   26│   27│   28│   29│   30│   31│   32│   33│   34│   35│   36│
  152.  │  428│  404│  381│  360│  339│  320│  302│  285│  269│  254│  240│  226│
  153.  │  C-2│  C#2│  D-2│  D#2│  E-2│  F-2│  F#2│  G-2│  G#2│  A-2│  A#2│  B-2│
  154.  ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤
  155.  │   37│   38│   39│   40│   41│   42│   43│   44│   45│   46│   47│   48│
  156.  │  214│  202│  190│  180│  170│  160│  151│  143│  135│  127│  120│  113│
  157.  │  C-3│  C#3│  D-3│  D#3│  E-3│  F-3│  F#3│  G-3│  G#3│  A-3│  A#3│  B-3│
  158.  ├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤
  159.  │   49│   50│   51│   52│   53│   54│   55│   56│   57│   58│   59│   60│
  160.  │  107│  101│   95│   90│   85│   80│   75│   71│   67│   63│   60│   56│
  161.  │  C-4│  C#4│  D-4│  D#4│  E-4│  F-4│  F#4│  G-4│  G#4│  A-4│  A#4│  B-4│
  162.  └─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┘
  163. A zero is stored in the event a period was not found or approximated or when
  164. there is not an actual period being played.  The note field requires 6 bits.
  165.  
  166. Version 0 only handles 31 instruments.  Thus, the instrument field requires
  167. 5 bits.
  168.  
  169. The special effect field can range from 0 to F in hexadecimal and requires
  170. 4 bits.
  171.  
  172. The special effects data field is a whole byte and can have 8 bits.
  173.  
  174. Thus, with this information, we are able to construct the following bitfields:
  175.           Byte  1                  Byte  2                  Byte  3
  176.       7 6 5 4 3 2 1 0          7 6 5 4 3 2 1 0          7 6 5 4 3 2 1 0
  177.       │ │ └─────────┘          └──┬──┘ └─────┘          └─────────────┘
  178.       │ │     Note                │     Effect            Effect Data
  179.       │ └ Bit 5 of instrument     └── Bits 1-4 of the instrument
  180.       │
  181.       └── Follow previous line bit.
  182.  
  183. Reconstruction algorithms are as follows:
  184.  
  185.     Get the ┌───Get Byte 1
  186.     Note    └───AND it with binary 00111111b
  187.  
  188.             ┌───Get Byte 1 again.
  189.             │   AND it with binary 01000000b
  190.             │   Shift it right twice.
  191.     Get the │   Store as result #1.
  192.     Instr.  │   Get Byte 2.
  193.             │   AND it with binary 11110000b
  194.             │   Shift it right four times.
  195.             └───OR it with Result #1.
  196.  
  197.     Get the ┌───Get Byte 2 again.
  198.     Effect. └───AND it with binary 00001111b
  199.  
  200.     Efx Data────Get Byte 3.
  201.  
  202. Bit 7 requires a bit of additional explanation.  As aforementioned, patterns
  203. can be a MINIMUM of 253 bytes smaller than their MOD counterparts.  This is
  204. due to three special compression methods.
  205.  
  206. One)  Instead of ordering the patterns as Track 1, Track 2, Track 3, Track 4,
  207.         then Track 1, Track 2 again and so on until the end, the Tracks are
  208.         grouped together as all of Track 1 and all of Track 2 and so on.
  209.  
  210. Two)  If a note appears on row 0 and another note appears on row 5 with
  211. Three)  nothing in between, why not get rid of the blank rows?  For instance,
  212.         a situation such as this may appear as:
  213.  
  214.            Module Track             MOD Data            PS16 Data
  215.            00  C-1 01 F06          0358 1F06             8D 1F 06
  216.            01  --- 00 000          0000 0000
  217.            02  --- 00 000          0000 0000
  218.            03  --- 00 000          0000 0000
  219.            04  --- 00 000          0000 0000
  220.            05  E-3 03 C40          00AA 3C40          05 29 3C 40
  221.            06  E-3 01 A01          00AA 1A01             A9 1A 01
  222.            07  --- 00 000          0000 0000
  223.                    .                   .
  224.                    .                   .
  225.                    .                   .
  226.            63  --- 00 000          0000 0000                   FF
  227.                                 ────────────       ──────────────
  228.                                   1024 bytes             11 bytes
  229.  
  230.       Two and three are combined for two reasons.  As can be seen, MODs
  231.         store extra information for blank pattern lines.  This was
  232.         remedied in PS16 by two methods.  If the jump between one line
  233.         and another was greater than one, then the new line number was
  234.         written out (see row 5 of the PS16 Data, first byte).  However,
  235.         if the two lines were right by each other (5 & 6), then the
  236.         pattern row was not written out.
  237.  
  238.       Here's why.  As you recall from the above diagrams of PS16 notes,
  239.         it can be seen that Bit 7 is named "Follow Previous Line."  This
  240.         means that if the new PS16 line follows the previous line, there
  241.         is no need to store a row number in front.  All that needs be
  242.         done is have this bit set.
  243.  
  244.       Let's break down the PS16 data from above:
  245.  
  246.         Row 0: 8D 1F 06    -- Bit 7 is set here.  The first line of a pattern
  247.                               is always assumed to follow Byte 255 (which is
  248.                               what the line counter is initialized).  A byte
  249.                               of 255 plus 1 = 0.  Otherwise, the 0Dh in 8Dh
  250.                               specifies note 0Dh, which is C-1.  The 1F 06
  251.                               specifies instrument 1, special effect F (speed)
  252.                               06.
  253.         Row 5: 05 29 3C 40 -- Since there was a large skip here, and row 5
  254.                               does not immediately follow row 0, the 5 was
  255.                               stored in first here.  How does the interpreter
  256.                               know the difference between a row number and
  257.                               a note?  A note ALWAYS follows a row number
  258.                               and if a row number is not present, then bit 7
  259.                               MUST be set in the note.  Bit 7 is NEVER set
  260.                               in a row except for the ONE exception below.
  261.         Row 6: A9 1A 01    -- Immediately follows Row 5, so bit 7 is set.
  262.         Final
  263.          Byte: FF          -- This is a -1 which means terminate track.
  264.  
  265. A -1 always terminates the current track.  Bear in mind that ALL 16 tracks
  266. are stored per pattern, so if converting from a MOD, tracks 5-16 are
  267. automatically set to -1.
  268.  
  269. The discussion thus far has been centered around the general pattern format.
  270. Two other factors are introduced into the PS16 pattern.  The format of a
  271. pattern is this:
  272.  
  273.         Byte    Name            Description
  274.        ──────  ─────────────── ────────────────────────────────────────
  275.          0-1   Size     (Word) Size is the total size of the ENTIRE
  276.                                 pattern, INCLUDING bytes 0 through 2.
  277.                                 This number is paragraph aligned.  This
  278.                                 means that if a pattern is 253 bytes
  279.                                 long, its actual size is 256.  If the
  280.                                 size is 256, no change takes place.
  281.            2   NumLines (Byte) This is the number of pattern lines in
  282.                                 the pattern.  If a pattern only uses
  283.                                 30 lines and the pattern must terminate
  284.                                 to the next pattern at this point,
  285.                                 store a 30 here.  Otherwise, ALWAYS store
  286.                                 a 64 so the entire pattern will play.
  287.         3-??   Track           This is Track 1, terminated with a -1.
  288.                                 The other tracks follow immediately after
  289.                                 this.
  290.  
  291.  
  292. See routine MOD_LoadSavePatterns in MODLOAD.ASM.
  293.  
  294. ╒══════════════════════════════════════════════════════════════════════════════╕
  295. │ PS16 Sample Format, Version 0:                                               │
  296. ╘══════════════════════════════════════════════════════════════════════════════╛
  297. Only the digital sample format has been defined thus far.  In order to appeal
  298. to archivers more, the sample storage format has changed.  Instead of being
  299. stored as raw data as in all trackers I am aware of, the format was changed
  300. to use "deltas."  The algorithm for converting a sample to deltas is:
  301.  
  302.                 Get the number of sample bytes to process.
  303.                   Call this SamplesLeft.
  304.  
  305.                 Set Delta counter to 0.
  306.  
  307.                 DO
  308.                   Get a byte from the buffer.
  309.                   Store the byte in Temp.
  310.                   Subtract the Delta counter from the byte.
  311.                   Store it in the buffer.
  312.                   Move the Temp byte into the Delta Counter
  313.                   Decrement SamplesLeft.
  314.                 WHILE(SamplesLeft <> 0)
  315.  
  316. See routine MOD_ConvertSample in MODLOAD.ASM.
  317.  
  318. The technique for conversion back to the raw data is:
  319.  
  320.                 Get the number of sample bytes to process.
  321.                   Call this SamplesLeft.
  322.  
  323.                 Set Delta counter to 0.
  324.  
  325.                 DO
  326.                   Get a byte from the buffer.
  327.                   Add onto the byte the Delta Counter.
  328.                   Store the byte in Delta Counter.
  329.                   Store the byte in Temp.
  330.                   Decrement SamplesLeft.
  331.                 WHILE(SamplesLeft <> 0)
  332.  
  333. ╒══════════════════════════════════════════════════════════════════════════════╕
  334. │ PS16 Comment Format, Version 0:                                              │
  335. ╘══════════════════════════════════════════════════════════════════════════════╛
  336. This format resembles an ILBM format.  Only two things are currently defined
  337. in this format.
  338.  
  339.         Signature     Description
  340.         ─────────     ─────────────────────────────────────────────────
  341.           INST        Immediately following this four character
  342.                         identifier is a byte indicating the length of
  343.                         each instrument name.  After that is a byte
  344.                         describing how many samples names are stored in
  345.                         this text.  Example for the MOD format:
  346.  
  347.                 I N S T [Chr 22] [Chr 2, normally 31]
  348.                 [Instrument name #1 padded to 22 chars]
  349.                 [Instrument name #2 padded to 22 chars]
  350.  
  351. See MOD_ConvertComments in MODLOAD.ASM.
  352.  
  353.           TEXT        Immediately following this four character
  354.                         identifier is a WORD describing the length
  355.                         of the upcoming text.
  356.  
  357.  
  358.